﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronSuit.DataMap;
using System.Collections;
using IronSuit.Utility;
using System.Threading;

namespace IronSuit.BaseData.Log
{
	public class LogThread : ILog
    {
		static Queue<ISLog> LogQueue = new Queue<ISLog>();
		static Thread LogT;
		static LogThread()
		{
			LogT = new Thread(WriteLog);
			LogT.IsBackground = true;
			LogT.Start();
		}		
		#region ILog Members
		public void Log(LogType lt, string logger, string message, Exception ex)
		{
			ISLog Log = new ISLog();
			Log.Logger = logger;
			Log.LogLevel = lt.ToString();
			Log.LogMessage = message;
			Log.LogTime = DateTime.Now;
			Log.LogUser = SessionHelper.UserId;
			if (ex != null)
				Log.LogException = ex.StackTrace;
			LogQueue.Enqueue(Log);
		}
		#endregion
		static void WriteLog()
		{
			while (true)
			{
				try
				{
					using (DBFactory DBF = new DBFactory())
					{
						ISLog l = LogQueue.Dequeue();
						if (l != null)
							DBF.Insert(l);
					}
				}
				catch
				{
				}
				Thread.Sleep(5000);
			}
		}
	}
}
